using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelBuffItem : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelBuffItem
        {
            #region ObjectBinding Generate

            public ExImage icon { protected set; get; }
            public ExText count { protected set; get; }
            public ExButton btn { protected set; get; }
            public ExText time { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExImage>("icon", out var __tbv0);
                icon = __tbv0;
                __binding.TryGetVariableValue<ExText>("count", out var __tbv1);
                count = __tbv1;
                __binding.TryGetVariableValue<ExButton>("btn", out var __tbv2);
                btn = __tbv2;
                __binding.TryGetVariableValue<ExText>("time", out var __tbv3);
                time = __tbv3;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        protected UIB_UIN_LevelBuffItem ui { get; set; }

        #region Override

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelBuffItem();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _buffIcon = ui.icon;
            _stackCount = ui.count;
            _countDown = ui.time;
            _canvasGroup = gameObject.GetOrAdd<CanvasGroup>();
            ui.btn.onClick.AddListener(OnBuffClick);
            isUpdate = true;
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        public void ShowByData(LevelBuffData buffData)
        {
            Show();
            Refresh(buffData);
        }

        protected override void OnDispose()
        {
            CodeBasedBlinkEffectGenerator.Instance.Destroy(_canvasGroup);
        }

        protected override void OnUpdate(float dt, float unscaledDt)
        {
            UpdateBlinkEffect();
            if (_buffData.endTime < GameServerModule.Instance.ServerTimestamp)
            {
                _countDown.text = string.Empty;
                return;
            }

            _countDown.text = APIUIConst.SecondsToHMS(_endTime - GameServerModule.Instance.ServerTimestamp);
        }

        #endregion

        #region BlinkEffect

        private void UpdateBlinkEffect()
        {
            var leftSec = _endTime - GameServerModule.Instance.ServerTimestamp;
            if (leftSec > BlinkThreshold)
            {
                CodeBasedBlinkEffectGenerator.Instance.Destroy(_canvasGroup);
                return;
            }

            if (leftSec <= BlinkThreshold)
            {
                CodeBasedBlinkEffectGenerator.Instance.Generate(_canvasGroup, 1f, 1f, 0.5f);
            }
        }

        #endregion

        #region Method

        private void Refresh(LevelBuffData buffData)
        {
            SetImageSprite(_buffIcon, buffData.cfgBuff.Icon_fullPath);
            _stackCount.text = buffData.stack > 1 ? buffData.stack.ToString() : string.Empty;
            _endTime = buffData.endTime;
            _buffData = buffData;
            UpdateBlinkEffect();
        }

        private void OnBuffClick()
        {
            this.OpenUI<UILevelBuff>(UD_LevelBuff.Create(_buffData));
        }

        #endregion

        #region Field

        private const float BlinkThreshold = 5f;
        private LevelBuffData _buffData;
        private long _endTime;

        private ExImage _buffIcon;
        private ExText _stackCount;
        private ExText _countDown;

        private CanvasGroup _canvasGroup;

        #endregion
    }
}
